package com.ruoyi.member.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.member.domain.MemberCollection;
import com.ruoyi.member.service.ICollectionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收藏管理Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/collection")
public class CollectionController extends BaseController
{
    @Autowired
    private ICollectionService collectionService;

    /**
     * 查询收藏列表
     */
    @PreAuthorize("@ss.hasPermi('member:collection:list')")
    @GetMapping("/list")
    public TableDataInfo list(MemberCollection collection)
    {
        startPage();
        List<MemberCollection> list = collectionService.selectCollectionList(collection);
        return getDataTable(list);
    }

    /**
     * 新增收藏
     */
    @PostMapping
    public AjaxResult add(@RequestBody MemberCollection collection)
    {
        // 检查是否已收藏
        if (collectionService.checkCollected(collection.getUserId(), collection.getShortId().longValue())) {
            return AjaxResult.error("该影视已收藏");
        }
        return toAjax(collectionService.insertCollection(collection));
    }

    /**
     * 删除收藏
     */
    @PreAuthorize("@ss.hasPermi('member:collection:remove')")
    @Log(title = "收藏管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{collectionIds}")
    public AjaxResult remove(@PathVariable Long[] collectionIds)
    {
        return toAjax(collectionService.deleteCollectionByIds(collectionIds));
    }

    /**
     * 取消收藏
     */
    @DeleteMapping("/cancel")
    public AjaxResult cancelCollection(@RequestBody MemberCollection collection)
    {
        return toAjax(collectionService.cancelCollection(collection.getUserId(), collection.getShortId().longValue()));
    }

    /**
     * 检查是否已收藏
     */
    @GetMapping("/check")
    public AjaxResult checkCollected(Long memberId, Long movieId)
    {
        return AjaxResult.success(collectionService.checkCollected(memberId, movieId));
    }

    /**
     * 获取用户收藏列表
     */
    @GetMapping("/member/{memberId}")
    public AjaxResult getMemberCollections(@PathVariable("memberId") Long memberId)
    {
        MemberCollection collection = new MemberCollection();
        collection.setUserId(memberId);
        List<MemberCollection> list = collectionService.selectCollectionList(collection);
        return AjaxResult.success(list);
    }

    /**
     * 导出收藏列表
     */
    @PreAuthorize("@ss.hasPermi('member:collection:export')")
    @Log(title = "收藏管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MemberCollection collection)
    {
        List<MemberCollection> list = collectionService.selectCollectionList(collection);
        ExcelUtil<MemberCollection> util = new ExcelUtil<MemberCollection>(MemberCollection.class);
        util.exportExcel(response, list, "收藏数据");
    }
}